package com.example.service;

import com.example.bean.Position;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

@Service("positionService")
public class PositionService {

    @Autowired
    private SolrClient  solrClient;

    public List<Position> queryPositions(String query) throws IOException, SolrServerException {
        SolrQuery  solrQuery = new SolrQuery();

        solrQuery.setQuery(query);

        QueryResponse queryResponse = solrClient.query(solrQuery);
        if (queryResponse == null){
            return  null;
        }
        List<Position>  positionList = queryResponse.getBeans(Position.class);

        //美女多、员工福利好
        if(positionList.size()<5){
            SolrQuery solrQuery1 = new SolrQuery();
            solrQuery1.setQuery("positionAdvantage:美女多、员工福利好");
            solrQuery1.setRows(5-positionList.size());
            QueryResponse othreQueryResponse = solrClient.query(solrQuery1);
            if (queryResponse != null){
                List<Position>  otherPositionList = othreQueryResponse.getBeans(Position.class);
                positionList.addAll(otherPositionList);
            }
        }
        return  positionList;
    }


}
